Raphael.fn.line = function (x1, y1, x2, y2) {
	return this.path("M"+x1+" "+y1+"L"+x2+" "+y2);
};

Raphael.fn.label = function (x, y, text, textAnchor, fontColor, fontSize) {
	if(textAnchor == null){
		textAnchor= 'start';
	}
	if(fontColor == null){
		fontColor = '#000';
	}
	if(fontSize == null){
		fontSize = 16;
	}

	var label = this.text(x, y, text);
	label.attr({
		"font-size": fontSize,
		'text-anchor': textAnchor,
		'fill': fontColor
	});
    return label;
};
$(function() {
	
    $('#datetimepicker').datetimepicker({
    	 format: 'YYYY-MM-DD HH:mm:ss'
    });
    
    $("#defaultForm").submit(function(e) {
    	console.log(e);
		$.ajax({ 
			url : $(this).attr('action') , 
			type: "POST", 
			data: $("#defaultForm :input").serializeArray(),
			success: function(data) {
				createMingPan(data);
			}
		}); 
        return false;
    });
    
    function paintGong(gong, x, y){
    	var type = $('input[name=type]:checked', '#defaultForm').val();
    	paper.label(x + 195, y+ 165, gong.changShengShen, 'end');
    	paper.label(x + 195, y+ 185, gong.gongGan + gong.gongZhi, 'end');
    	
    	if(type == 'tianPan'){
    		paper.label(x + 100, y+ 165, gong.startDaXian+"-"+gong.endDaXian, 'middle');
    		paper.label(x + 100, y+ 185, gong.shiErGong+"宫", 'middle', '#f00');
    	}else if(type == 'daXianPan'){
    		paper.label(x + 100, y+ 145, gong.startDaXian+"-"+gong.endDaXian, 'middle');
    		paper.label(x + 100, y+ 165, gong.shiErGong+"宫", 'middle', '#f00');
        	paper.label(x + 100, y+ 185, "运"+gong.daYunShiErGong, 'middle', "#0f0");
    	}else if(type == 'liuNianPan'){
    		paper.label(x + 100, y+ 125, gong.startDaXian+"-"+gong.endDaXian, 'middle');
    		paper.label(x + 100, y+ 145, gong.shiErGong+"宫", 'middle', '#f00');
        	paper.label(x + 100, y+ 165, "运"+gong.daYunShiErGong, 'middle', "#0f0");
        	paper.label(x + 100, y+ 185, "流"+gong.liuNianShiErGong, 'middle', "#00f");
    	}
    	
    	paper.label(x+5, y+ 145, gong.boShiShen);
    	paper.label(x+5, y+ 165, gong.taiSuiShen);
    	paper.label(x+5, y+ 185, gong.jiangQianShen);
    	
    	$.each(gong.zhengYaoList, function(index, data){
    		var offset = y+10;
    		if(index != 0){
    			offset += 20*index;
    		}
    		paper.label(x+100, offset, data.starName+"("+ data.starMX+")", 'middle');
    		var i = 1;
    		if(data.siHua != null){
    			paper.label(x+115+(17*i), offset, data.siHua, 'start', "#f00");
    			i++;
    		}
    		if(type == 'daXianPan' || type == 'liuNianPan'){
	    		if(data.daYunSiHua != null){
	    			paper.label(x+115+(17*i), offset, data.daYunSiHua, 'start', "#0f0");
	    			i++;
	    		}
	    		if(type == 'liuNianPan'){
		    		if(data.liuNianSiHua != null){
		    			paper.label(x+115+(17*i), offset, data.liuNianSiHua, 'start', "#00f");
		    		}
	    		}
    		}
    	});
    	
    	var zhengYaoHeight = gong.zhengYaoList != null ? gong.zhengYaoList.length*20 : 0;
    	var fuYaoHeight =  gong.fuYaoList != null ? gong.fuYaoList.length*20 : 0;
    	var yunYaoHeight =  gong.yunYaoList != null ? gong.yunYaoList.length*20 : 0;
    	
    	$.each(gong.fuYaoList, function(index, data){
    		var offset = y+10 + zhengYaoHeight;
    		if(index != 0){
    			offset += 20*index;
    		}
    		paper.label(x+170, offset, data.starName+"("+ data.starMX+")", 'middle');
    		var i = 1;
    		if(data.siHua != null){
    			paper.label(x+140-(17*i), offset, data.siHua, 'start', "#f00");
    			i++;
    		}
    		if(type == 'daXianPan' || type == 'liuNianPan'){
	    		if(data.daYunSiHua != null){
	    			paper.label(x+140-(17*i), offset, data.daYunSiHua, 'start', "#0f0");
	    			i++;
	    		}
	    		if(type == 'liuNianPan'){
		    		if(data.liuNianSiHua != null){
		    			paper.label(x+140-(17*i), offset, data.liuNianSiHua, 'start', "#00f");
		    		}
	    		}
    		}

    	});
    	
    	$.each(gong.zaYaoList, function(index, data){
    		var offset = y + 125;
    		if(index != 0){
    			offset -= 20*index;
    		}
    		paper.label(x+5, offset, data.starName+"("+ data.starMX+")", 'start');
    	});
    	
    	if(type == 'daXianPan' || type == 'liuNianPan'){
        	$.each(gong.yunYaoList, function(index, data){
        		var offset = y+10 + zhengYaoHeight + fuYaoHeight;
        		if(index != 0){
        			offset += 20*index;
        		}
        		paper.label(x+165, offset, data.starName, 'start', "#0f0");
        	});	
    		if(type == 'liuNianPan'){
            	$.each(gong.liuYaoList, function(index, data){
            		var offset = y+10 + zhengYaoHeight + fuYaoHeight + yunYaoHeight;
            		console.log(offset);
            		if(index != 0){
            			offset += 20*index;
            		}
            		paper.label(x+165, offset, data.starName, 'start', "#00f");
            	});	
    		}
    	}
    }
    
    function createMingPan(data){
    	paper = Raphael("canvas",  window.innerWidth, window.innerHeight);
    	//画盘
    	paper.rect(0, 0, 800, 800);
    	paper.line(0,200,800,200);
    	paper.line(0,600,800,600);
    	paper.line(200,0,200,800);
    	paper.line(600,0,600,800);
    	paper.line(400,0,400,200);
    	paper.line(400,600,400,800);
    	paper.line(0,400,200,400);
    	paper.line(600,400,800,400);
    	
    	//写基本信息
    	paper.label(250, 250, "农历  " + data.basicInfo.tianGan+data.basicInfo.diZhi + "年" + (data.basicInfo.month+1) + "月" + data.basicInfo.day + "日" + data.basicInfo.shiChen + "时生");
    	paper.label(250, 280, "天盘 " + "年龄 " + data.basicInfo.age +"岁   " + data.basicInfo.yinYang + data.basicInfo.sex + " "+ data.basicInfo.wuXing +"局");
    	paper.label(250, 310, "命宫在" + data.mingGong + " 身宫在" + data.shenGong);
    	paper.label(250, 340, "命主" + data.basicInfo.mingZhu + " 身主" + data.basicInfo.shenZhu+ " 子年斗君在" + data.basicInfo.ziNianDouJun);
    	paper.label(250, 370, "现行大限为" + data.basicInfo.daXian + "宫");
    	paper.label(250, 400, "流年天干为" + data.basicInfo.liuNianTianGan + " 流年地支为" + data.basicInfo.liuNianDiZhi + " 流年斗君在" + data.basicInfo.liuNianDouJun);
    	
    	//画十二宫
    	paintGong(data.gongList[0], 400, 600);
    	paintGong(data.gongList[1], 200, 600);
    	paintGong(data.gongList[2], 0, 600);
    	paintGong(data.gongList[3], 0, 400);
    	paintGong(data.gongList[4], 0, 200);
    	paintGong(data.gongList[5], 0, 0);
    	paintGong(data.gongList[6], 200, 0);
    	paintGong(data.gongList[7], 400, 0);
    	paintGong(data.gongList[8], 600, 0);
    	paintGong(data.gongList[9], 600, 200);
    	paintGong(data.gongList[10], 600, 400);
    	paintGong(data.gongList[11], 600, 600);
    }
});
